package com.lib.common.view.paypwd;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lib.common.R;

/**
 * @author FengLing
 * Created on 2017-08-07 23:02
 */
public class PayDialog extends PopupWindow implements View.OnClickListener {
    private int mMaxPwdLength;
    private char[] mPwdList;
    private int mSize;
    // private ArrayList<String> mPwdList = new ArrayList<>();
    private PayPwdView mPayPwd;
    private TextView mP1;
    private TextView mP2;
    private TextView mP3;
    private TextView mP4;
    private TextView mP5;
    private TextView mP6;
    private TextView mP7;
    private TextView mP8;
    private TextView mP9;
    private TextView mP0;
    private TextView mDel;

    private EventCallBack mEventCallBack;
    private View mView;

    public interface EventCallBack {

        void onForgetPwd(PayDialog payDialog);

        void onInputFinish(PayDialog payDialog, String pwdStr);
    }

    public void setEventListener(EventCallBack l) {
        mEventCallBack = l;
    }

    public PayDialog(Context context) {
        super(context);
    }

    public PayDialog(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PayDialog(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mView = LayoutInflater.from(context).inflate(R.layout.dialog_input_paypwd, null);
        setWidth(LinearLayout.MarginLayoutParams.MATCH_PARENT);
        setHeight(LinearLayout.MarginLayoutParams.WRAP_CONTENT);
        setAnimationStyle(R.style.PopFadedAnimStyle);
        setBackgroundDrawable(new ColorDrawable());
        setContentView(mView);
        setOutsideTouchable(true);

        mPayPwd = mView.findViewById(R.id.pay_pwd);
        mP1 = mView.findViewById(R.id.p1);
        mP2 = mView.findViewById(R.id.p2);
        mP3 = mView.findViewById(R.id.p3);
        mP4 = mView.findViewById(R.id.p4);
        mP5 = mView.findViewById(R.id.p5);
        mP6 = mView.findViewById(R.id.p6);
        mP7 = mView.findViewById(R.id.p7);
        mP8 = mView.findViewById(R.id.p8);
        mP9 = mView.findViewById(R.id.p9);
        mP0 = mView.findViewById(R.id.p0);
        mDel = mView.findViewById(R.id.del);

        mP1.setOnClickListener(this);
        mP2.setOnClickListener(this);
        mP3.setOnClickListener(this);
        mP4.setOnClickListener(this);
        mP5.setOnClickListener(this);
        mP6.setOnClickListener(this);
        mP7.setOnClickListener(this);
        mP8.setOnClickListener(this);
        mP9.setOnClickListener(this);
        mP0.setOnClickListener(this);
        mDel.setOnClickListener(this);
        mView.findViewById(R.id.forget_paypwd).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (mEventCallBack != null) {
                    mEventCallBack.onForgetPwd(PayDialog.this);
                }
            }
        });
        setLen(6);
    }

    private void setLen(int length) {
        mPwdList = new char[length];
        mMaxPwdLength = length;
        mPayPwd.setMaxLength(length);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.p1) {
            mPwdList[mSize] = '1';
            mSize++;
            // mPwdList.add("1");
        } else if (id == R.id.p2) {
            mPwdList[mSize] = '2';
            mSize++;
            // mPwdList.add("2");
        } else if (id == R.id.p3) {
            mPwdList[mSize] = '3';
            mSize++;
            // mPwdList.add("3");

        } else if (id == R.id.p4) {
            mPwdList[mSize] = '4';
            mSize++;
            // mPwdList.add("4");

        } else if (id == R.id.p5) {
            mPwdList[mSize] = '5';
            mSize++;
            // mPwdList.add("5");

        } else if (id == R.id.p6) {
            mPwdList[mSize] = '6';
            mSize++;
            // mPwdList.add("6");

        } else if (id == R.id.p7) {
            mPwdList[mSize] = '7';
            mSize++;
            // mPwdList.add("7");

        } else if (id == R.id.p8) {
            mPwdList[mSize] = '8';
            mSize++;
            // mPwdList.add("8");

        } else if (id == R.id.p9) {
            mPwdList[mSize] = '9';
            mSize++;
            // mPwdList.add("9");

        } else if (id == R.id.p0) {
            mPwdList[mSize] = '0';
            mSize++;
            // mPwdList.add("0");

        } else if (id == R.id.del) {
            mSize--;
            // mPwdList.remove(mPwdList.size() - 1);

        }
        // int mSize = mPwdList.size();
        if (mSize == mMaxPwdLength && mEventCallBack != null) {
            mEventCallBack.onInputFinish(PayDialog.this, new String(mPwdList));
            // Toast.makeText(sContext, mPwdList.element(), Toast.LENGTH_SHORT).show();
        }
        mP1.setEnabled(mSize < mMaxPwdLength);
        mP2.setEnabled(mSize < mMaxPwdLength);
        mP3.setEnabled(mSize < mMaxPwdLength);
        mP4.setEnabled(mSize < mMaxPwdLength);
        mP5.setEnabled(mSize < mMaxPwdLength);
        mP6.setEnabled(mSize < mMaxPwdLength);
        mP7.setEnabled(mSize < mMaxPwdLength);
        mP8.setEnabled(mSize < mMaxPwdLength);
        mP9.setEnabled(mSize < mMaxPwdLength);
        mP0.setEnabled(mSize < mMaxPwdLength);
        mDel.setEnabled(mSize > 0);
        mPayPwd.refreshStatus(mSize);
    }

    public void show() {
        showAtLocation(mView, Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void dismiss() {
        mSize = 0;
        // mPwdList.clear();
        mP1.setEnabled(true);
        mP2.setEnabled(true);
        mP3.setEnabled(true);
        mP4.setEnabled(true);
        mP5.setEnabled(true);
        mP6.setEnabled(true);
        mP7.setEnabled(true);
        mP8.setEnabled(true);
        mP9.setEnabled(true);
        mP0.setEnabled(true);
        mDel.setEnabled(false);
        mPayPwd.refreshStatus(0);
        super.dismiss();
    }
}